#include "my_stdio.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


mFILE *mfopen(const char *filename, const char *mode)
{
    int fd = -1;
    if(strcmp(mode,"r") == 0)
    {
        fd = open(filename, O_RDONLY);
    }
    else if(strcmp(mode, "w") == 0)
    {
        fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    }
    else if(strcmp(mode, "a") == 0)
    {
        fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0666);
    }
    if(fd < 0) return NULL;
    mFILE *mf = (mFILE*)malloc(sizeof(mFILE));
    if(!mf)
    {
        close(fd);
        return NULL;
    }

    mf->fileno = fd;
    mf->flag = FFLUSH_LINE;
    mf->size = 0;
    mf->cap = SIZE;

    return mf;
}

//int mfwrite(const void *ptr, int num, mFILE *stream);


//void mfflush(mFILE *stream);
//void mfclose(mFILE *stream);
